#!/usr/bin env python
# -*- coding: utf-8 -*-
###########################################################################
#   Copyright (C) 2008 by Serj Poltavsky                                  #
#   serge.uliss at gmail.com                                              #
#                                                                         #
#   This program is free software; you can redistribute it and/or modify  #
#   it under the terms of the GNU General Public License as published by  #
#   the Free Software Foundation; either version 3 of the License, or     #
#   (at your option) any later version.                                   #
#                                                                         #
#   This program is distributed in the hope that it will be useful,       #
#   but WITHOUT ANY WARRANTY; without even the implied warranty of        #
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         #
#   GNU General Public License for more details.                          #
#                                                                         #
#   You should have received a copy of the GNU General Public License     #
#   along with this program. If not, see <http://www.gnu.org/licenses/>   #
###########################################################################

from xml.dom import minidom
from node import *

class Spumux(Node):
    def __init__(self):
        Node.__init__(self, 'subpictures')
        self.__dom = None

    ''' Returns XML DOM '''
    def get_dom(self):
        return self.__dom

    def save(self, filename):
        f = open(filename, 'w')
        f.write(self.dom.toprettyxml(encoding='utf-8'))
        f.close()

    def spu_files(self):
        files = []
        for stream in self:
            files.extend(stream.spu_files())
        return files

    def update(self):
        self.__dom = minidom.Document()
        root = Node.update(self, self.__dom, self.__dom)
        for stream in self:
            stream.update(self.__dom, root)
        return root

    dom = property(get_dom)

class SpumuxStream(Node):
    def __init__(self, image = None, highlight = None, select = None):
        Node.__init__(self, 'stream')
        self.start = 0
        self.end = 0
        self.image = image
        self.image_select = highlight
        self.image_highlight = select
        self.force = False

    def get_force(self):
        return self.__force

    def get_image(self):
        return self.__image

    def get_image_highlight(self):
        return self.__image_highlight

    def get_image_select(self):
        return self.__image_select

    def get_start(self):
        return self.__start

    def set_force(self, force):
        self.__force = bool(force)

    def set_image(self, image):
        self.__image = image

    def set_image_highlight(self, image):
        self.__image_highlight = image

    def set_image_select(self, image):
        self.__image_select = image

    def set_start(self, start):
        self.__start = start

    def spu_files(self):
        return [f for f in (self.image, self.image_select, self.image_highlight) if f]

    def update(self, dom, root):
        spumux = Node.update(self, dom, root)
        spu = dom.createElement('spu')
        spumux.appendChild(spu)

        if self.image is not None:
            spu.setAttribute('image', self.image)
        if self.image_select is not None:
            spu.setAttribute('select', self.image_select)
        if self.image_highlight is not None:
            spu.setAttribute('highlight', self.image_highlight)
        if self.force:
            spu.setAttribute('force', 'yes')
        else:
            spu.setAttribute('force', 'no')

        if self.start != 0:
            spu.setAttribute('start', seconds_to_str(self.start))

        if self.end != 0:
            spu.setAttribute('end', seconds_to_str(self.end))

        self.update_children(dom, spu)
        return spumux

    force = property(get_force, set_force)
    image = property(get_image, set_image)
    image_highlight = property(get_image_highlight, set_image_highlight)
    image_select = property(get_image_select, set_image_select)
    start = property(get_start, set_start)

class SpumuxButton(Node):
    def __init__(self, name, pos, size):
        Node.__init__(self, 'button')
        self.name = name
        self.pos = pos
        self.size = size

    def update(self, dom, parent):
        button = Node.update(self, dom, parent)
        if self.name:
            button.setAttribute('name', self.name)
        button.setAttribute('x0', str(self.pos[0]))
        button.setAttribute('y0', str(self.pos[1]))
        button.setAttribute('x1', str(self.pos[0] + self.size[0]))
        button.setAttribute('y1', str(self.pos[1] + self.size[1]))
        return button